# -*- encoding=utf8 -*-
__author__ = "yupf"

from airtest.core.api import *
# from poco.drivers.ios import iosPoco
# from easyjet_order import get_resp
# connect_device("iOS:///127.0.0.1:8300")

# poco = iosPoco()


def passenger_detail(poco, passengers: list):
    # 选择title
    if not passengers:
        raise ValueError("passengers cannot be empty!")

    pl = len(passengers)
    for i in range(pl):
        poco(f'adult_{i}_pax_title').child('Title', type='Button').click()
        poco('Table').offspring(passengers[i]['title']).click()

        # 输入 first name
        try:
            poco(f'adult_{i}_pax_firstName').offspring(value='First name', type='TextField').click()
            text(passengers[i]['first_name'], enter=False)
        except:
            content = poco(f'adult_{i}_pax_firstName').offspring(label='First name', type='TextField').attr('value')
            for _ in range(len(content)):
                text('\b', enter=False)
            text(passengers[i]['first_name'], enter=False)

        # 输入 last name
        if i > 0:
            swipe([0.5, 0.5], vector=(0.5, 0.2))

        try:
            poco(f'adult_{i}_pax_lastName').offspring(value='Last name', type='TextField').focus((0.5, 0.5)).click()
            text(passengers[i]['last_name'], enter=False)
        except:
            content = poco(f'adult_{i}_pax_lastName').offspring(label='Last name', type='TextField').attr('value')
            poco(f'adult_{i}_pax_lastName').offspring(label='Last name', type='TextField').focus((0.5, 0.5)).click()
            for _ in range(len(content)):
                text('\b', enter=False)
            text(passengers[i]['last_name'], enter=False)
        # 选择 aget at time of travel

        poco(f'adult_{i}_pax_ageSelectAdult').click()

        poco("Table").offspring(passengers[i]['age']).click()

    poco(label="Continue", type='Button').wait_for_appearance()
    poco(label="Continue", type='Button').click()


# order,_ = get_resp()
#
# passenger_detail(poco,order['passengers'])